﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;
using LevelEditor.Objects;
using LevelEditor.Objects.Tiles;
using LevelEditor.Dao;

namespace LevelEditor.Components.Properties
{
    public class PropertiesPanel : FlowLayoutPanel
    {
        Dictionary<Character.Variables, Control> charProperties;
        Dictionary<Item.Variables, Control> itemProperties;
        Dictionary<CTile.Variables, Control> cTileProperties;

        Dictionary<Door.Variables, Control> doorProperties;

        LevelEditor editor;

        private Button editCollisions;
        
        public PropertiesPanel()
            : base()
        {
            
        }

        public void setForm(LevelEditor editor)
        {
            this.editor = editor;
            
        }

        private void Initialize()
        {
            editCollisions = new Button();
            editCollisions.Text = "Edit Collisions";
            editCollisions.Width = this.Width;
            editCollisions.Height = 30;
            editCollisions.Dock = DockStyle.Top;
            editCollisions.Click += editCollisions_Click;

            this.Controls.Add(editCollisions);
        }

        void editCollisions_Click(object sender, EventArgs e)
        {
            EditCollisions editCollisionsDialog = new EditCollisions(Tile.CurrentSelectedTile);
            editCollisionsDialog.ShowDialog();

            editCollisionsDialog = null;   
        }

        public void ShowProperties()
        {
            this.HideProperties();
            Initialize();
            switch (Tile.CurrentSelectedTile.myType)
            {
                case Tile.TileType.Character:
                    InitializeCharComponents((Character)Tile.CurrentSelectedTile);
                    ShowCharProperties((Character)Tile.CurrentSelectedTile);
                    break;
                case Tile.TileType.Item:
                    InitializeItemComponents((Item)Tile.CurrentSelectedTile);
                    ShowItemProperties((Item)Tile.CurrentSelectedTile);
                    break;
                case Tile.TileType.CommonTile:
                    InitializeCTileComponents((CTile)Tile.CurrentSelectedTile);
                    showCTileProperties((CTile)Tile.CurrentSelectedTile);
                    break;
                case Tile.TileType.Door:
                    InitializeDoorComponents((Door)Tile.CurrentSelectedTile);
                    ShowDoorProperties((Door)Tile.CurrentSelectedTile);
                    break;
            }
        }

        public void HideProperties()
        {
            this.Controls.Clear();
        }

        public void HideProperties(Tile tile)
        {
            switch (tile.myType)
            {
                case Tile.TileType.Character:
                    //HideCharProperties((Character)tile);
                    break;
                case Tile.TileType.Item:
                    //HideItemProperties((Item)tile);
                    break;
                case Tile.TileType.CommonTile:
                    //showCTileProperties();
                    break;
                case Tile.TileType.Door:
                    HideDoorProperties((Door)tile);
                    break;
            }

            Character.Variables[] variables = (Character.Variables[])Enum.GetValues(typeof(Character.Variables));
            int i = 0;
            for (int k = 0; k < this.Controls.Count; k++)
            {
                if (typeof(Label) == this.Controls[k].GetType()) continue;

                if ((this.Controls[k] as TextBox) != null)
                {
                    EventInfo eventInfo = ((TextBox)this.Controls[k]).GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[i] + "Changed", false);
                    eventInfo.RemoveEventHandler(((TextBox)this.Controls[k]), d);
                    i++;
                }
                else if ((this.Controls[k] as ComboBox) != null)
                {
                    EventInfo eventInfo = ((ComboBox)this.Controls[k]).GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[i] + "Changed", false);
                    eventInfo.RemoveEventHandler(((ComboBox)this.Controls[k]), d);
                    i++;
                }

            }
            this.Controls.Clear();
        }

        private void ShowCharProperties(Character tile)
        {
            Character.Variables[] variables = (Character.Variables[])Enum.GetValues(typeof(Character.Variables));
            for (int k = 0; k < variables.Length; k++)
            {
                Label label = new Label();
                label.Text = variables[k].ToString();
                if ((charProperties[variables[k]] as TextBox) != null)
                {
                    TextBox txt = (TextBox)charProperties[variables[k]];
                    EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                    eventInfo.RemoveEventHandler(txt, d);

                    txt.Text = typeof(Character).GetProperty(variables[k].ToString()).GetValue(tile).ToString();

                    txt.TextChanged += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), tile, typeof(Character).GetMethod(variables[k] + "Changed"));

                }
                else if ((charProperties[variables[k]] as ComboBox) != null)
                {
                    ComboBox txt = (ComboBox)charProperties[variables[k]];
                    //txt.GetType().GetEvent()
                    EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                    eventInfo.RemoveEventHandler(txt, d);
                }
                else if ((charProperties[variables[k]] as TableLayoutPanel) != null)
                {
                    TableLayoutPanel txt = (TableLayoutPanel)charProperties[variables[k]];
                    if (label.Text == Character.Variables.MyTrigger.ToString())
                    {
                        foreach (Control c in txt.Controls)
                        {
                            if ((c as Label) == null) continue;
                            (c as Label).MouseDoubleClick += new MouseEventHandler(tile.MyTriggerClicked);
                        }
                    }
                }

                this.Controls.Add(label);
                this.Controls.Add(charProperties[variables[k]]);
            }
        }

        private void ShowItemProperties(Item tile)
        {
            Item.Variables[] variables = (Item.Variables[])Enum.GetValues(typeof(Item.Variables));
            for (int k = 0; k < variables.Length; k++)
            {
                Label label = new Label();
                label.Text = variables[k].ToString();

                if ((itemProperties[variables[k]] as TextBox) != null)
                {
                    TextBox txt = (TextBox)itemProperties[variables[k]];
                    EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                    eventInfo.RemoveEventHandler(txt, d);
                    txt.Text = typeof(Item).GetProperty(variables[k].ToString()).GetValue(tile).ToString();
                    txt.TextChanged += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), tile, typeof(Item).GetMethod(variables[k] + "Changed"));
                }
                else if ((itemProperties[variables[k]] as ComboBox) != null)
                {
                    ComboBox txt = (ComboBox)itemProperties[variables[k]];
                    EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                    eventInfo.RemoveEventHandler(txt, d);
                }
                else if ((itemProperties[variables[k]] as TableLayoutPanel) != null)
                {
                    TableLayoutPanel txt = (TableLayoutPanel)itemProperties[variables[k]];

                    #region My Trigger
                    if (label.Text == Item.Variables.MyTrigger.ToString())
                    {
                        foreach (Control c in txt.Controls)
                        {
                            if ((c as Label) == null) continue;
                            (c as Label).MouseDoubleClick += new MouseEventHandler(tile.MyTriggerClicked);
                        }
                    }
                    #endregion
                    ///ADICIONAR UM EVENTO QUE PEGUE O VALOR DA COMBOBOX YES/NO
                    ///SE TIVER NO (ESCONDE AS LINHAS DAS OUTRAS TABELAS)
                    ///SE TIVER YES (MOSTRA AS LINHAS DAS OUTRAS TABELAS)
                }
                this.Controls.Add(label);
                this.Controls.Add(itemProperties[variables[k]]);
            }
        }

        private void showCTileProperties(CTile tile)
        {
            CTile.Variables[] variables = (CTile.Variables[])Enum.GetValues(typeof(CTile.Variables));
            for (int k = 0; k < variables.Length; k++)
            {
                Label label = new Label();
                label.Text = variables[k].ToString();

                if ((cTileProperties[variables[k]] as TextBox) != null)
                {
                    TextBox txt = (TextBox)cTileProperties[variables[k]];
                    EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                    eventInfo.RemoveEventHandler(txt, d);
                    txt.Text = typeof(Item).GetProperty(variables[k].ToString()).GetValue(tile).ToString();
                    txt.TextChanged += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), tile, typeof(CTile).GetMethod(variables[k] + "Changed"));
                }
                else if ((cTileProperties[variables[k]] as ComboBox) != null)
                {
                    ComboBox txt = (ComboBox)cTileProperties[variables[k]];
                    EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                    eventInfo.RemoveEventHandler(txt, d);
                }
                else if ((cTileProperties[variables[k]] as TableLayoutPanel) != null)
                {
                    TableLayoutPanel txt = (TableLayoutPanel)cTileProperties[variables[k]];

                    #region My Trigger
                    if (label.Text == CTile.Variables.MyTrigger.ToString())
                    {
                        foreach (Control c in txt.Controls)
                        {
                            if ((c as Label) == null) continue;
                            (c as Label).MouseDoubleClick += new MouseEventHandler(tile.MyTriggerClicked);
                        }
                    }
                    #endregion
                    ///ADICIONAR UM EVENTO QUE PEGUE O VALOR DA COMBOBOX YES/NO
                    ///SE TIVER NO (ESCONDE AS LINHAS DAS OUTRAS TABELAS)
                    ///SE TIVER YES (MOSTRA AS LINHAS DAS OUTRAS TABELAS)
                }
                this.Controls.Add(label);
                this.Controls.Add(cTileProperties[variables[k]]);
            }
        }

        public new void Refresh()
        {
            HideProperties();

            ShowProperties();
        }

        private void InitializeCharComponents(Character character)
        {
            charProperties = new Dictionary<Character.Variables, Control>();
            
            TextBox positionX = new TextBox();
            positionX.Text = "";
            
            TextBox positionY = new TextBox();
            positionY.Text = "";
                        
            TextBox walkSpeed = new TextBox();
            walkSpeed.Text = "";
            
            TextBox angleOfView = new TextBox();
            angleOfView.Text = "";
            
            TextBox perceptionArea = new TextBox();
            perceptionArea.Text = "";

            #region MyTrigger
            TableLayoutPanel tlpMyTrigger = new TableLayoutPanel();
            tlpMyTrigger.RowCount = character.MyTrigger.Count;
            tlpMyTrigger.ColumnCount = 2;
            tlpMyTrigger.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpMyTrigger.AutoScroll = true;
            
            for (int line = 0; line < character.MyTrigger.Count; line++)
            {
                Label l = new Label();
                l.Text = character.MyTrigger[line].Name;
                
                tlpMyTrigger.Controls.Add(l, 0, line);
                for (int column = 1; column <= character.MyTrigger.Count; column++)
                {
                    Label behavior = new Label();
                    tlpMyTrigger.Controls.Add(behavior, column, line);
                    behavior.Text = character.MyTrigger[line].MappedBehavior.Name;
                    if (character.MyTrigger[line].MappedBehavior.IsWalk)
                    {
                        MenuItem mItem = new MenuItem("Show Waypoints");
                        Behavior b = character.MyTrigger[line].MappedBehavior;
                        mItem.Click += new EventHandler((sender, e) => mItem_Click(sender, e, b));
                        behavior.ContextMenu = new ContextMenu(new MenuItem[] { mItem });
                    }
                }
            }
            #endregion

            TableLayoutPanel behaviorstTable = new TableLayoutPanel();
            behaviorstTable.RowCount = character.standAloneBehaviors.Count;
            behaviorstTable.ColumnCount = 2;
            behaviorstTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            behaviorstTable.AutoScroll = true;

            for (int l = 0; l < character.standAloneBehaviors.Count; l++)
            {
                Label label = new Label();
                label.Text = character.standAloneBehaviors[l].Name;
                
                Button btn = new Button();
                btn.Text = "D";
                btn.Width = 20;
                btn.Height = 20;
                
                Behavior b = character.standAloneBehaviors[l];
                btn.Click += new EventHandler((sender,e) => deleteStandAlnoBehavior(sender, e, b, character));
                label.DoubleClick += new EventHandler((sender, e) => alterLoneBehavior(sender, e, b));

                if (b.IsWalk)
                {
                    MenuItem mItem = new MenuItem("Show Waypoints");
                    mItem.Click += new EventHandler((sender,e) => mItem_Click(sender, e, b));
                    label.ContextMenu = new System.Windows.Forms.ContextMenu(new MenuItem[] { mItem });
                }

                behaviorstTable.Controls.Add(label, 0, l);
                behaviorstTable.Controls.Add(btn, 1, l);
            }

            //criar uma tabela, onde a coluna 1 são os nomes dos triggers e a coluna 2 tem os combobox dos behaviors
            TableLayoutPanel tlp = new TableLayoutPanel();

            List<Trigger> triggers = LevelEditorDAO.GetInstance().GetAllTriggers();
            List<Behavior> behaviors = LevelEditorDAO.GetInstance().GetAllBehavior();
            tlp.RowCount = triggers.Count;
            tlp.ColumnCount = 2;
            tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlp.AutoScroll = true;

            for (int line = 0; line < triggers.Count; line++)
            {
                Label l = new Label();
                l.Text = triggers[line].Name;

                ComboBox behavior = new ComboBox();
                behavior.Text = "";
                behavior.Items.Add("");
                foreach (Behavior b in behaviors)
                    behavior.Items.Add(b.Name);

                tlp.Controls.Add(l, 0, line);
                tlp.Controls.Add(behavior, 1, line);
            }

            charProperties.Add(Character.Variables.PositionX, positionX);
            charProperties.Add(Character.Variables.PositionY, positionY);
            charProperties.Add(Character.Variables.WalkSpeed, walkSpeed);
            charProperties.Add(Character.Variables.FieldOfView, angleOfView);
            charProperties.Add(Character.Variables.PerceptionArea, perceptionArea);
            charProperties.Add(Character.Variables.MyTrigger, tlpMyTrigger);
            charProperties.Add(Character.Variables.LoneBehavior, behaviorstTable);
        }

        void alterLoneBehavior(object sender, EventArgs e, Behavior b)
        {
            NewBehavior newBehaviorDialog = new NewBehavior(b);
            newBehaviorDialog.ShowDialog();
        }

        void deleteStandAlnoBehavior(object sender, EventArgs e, Behavior b, Tile tile)
        {
            tile.standAloneBehaviors.Remove(b);
        }

        /// <summary>
        /// clicou para mostrar os waypoints
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mItem_Click(object sender, EventArgs e, Behavior b)
        {
            this.editor.stage1.ShowWaypoints(b.Waypoints);
        }

        private void InitializeItemComponents(Item item)
        {
            itemProperties = new Dictionary<Item.Variables, Control>();

            TextBox positionX = new TextBox();
            positionX.Text = "";

            TextBox positionY = new TextBox();
            positionY.Text = "";

            List<Trigger> triggers = LevelEditorDAO.GetInstance().GetAllTriggers();
            #region My Trigger
            TableLayoutPanel tlpMyTrigger = new TableLayoutPanel();
            tlpMyTrigger.RowCount = item.MyTrigger.Count;
            tlpMyTrigger.ColumnCount = 1;
            tlpMyTrigger.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpMyTrigger.AutoScroll = true;

            for (int line = 0; line < item.MyTrigger.Count; line++)
            {
                Label l = new Label();
                l.Text = item.MyTrigger[line].Name;
                tlpMyTrigger.Controls.Add(l, 0, line);
            }
            #endregion

            #region StandAloneBehavior

            TableLayoutPanel behaviorstTable = new TableLayoutPanel();
            behaviorstTable.RowCount = item.standAloneBehaviors.Count;
            behaviorstTable.ColumnCount = 2;
            behaviorstTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            behaviorstTable.AutoScroll = true;

            for (int l = 0; l < item.standAloneBehaviors.Count; l++)
            {
                Label label = new Label();
                label.Text = item.standAloneBehaviors[l].Name;

                Button btn = new Button();
                btn.Text = "D";
                btn.Width = 20;
                btn.Height = 20;

                Behavior b = item.standAloneBehaviors[l];
                btn.Click += new EventHandler((sender, e) => deleteStandAlnoBehavior(sender, e, b, item));
                label.DoubleClick += new EventHandler((sender, e) => alterLoneBehavior(sender, e, b));

                if (b.IsWalk)
                {
                    MenuItem mItem = new MenuItem("Show Waypoints");
                    mItem.Click += new EventHandler((sender, e) => mItem_Click(sender, e, b));
                    label.ContextMenu = new System.Windows.Forms.ContextMenu(new MenuItem[] { mItem });
                }

                behaviorstTable.Controls.Add(label, 0, l);
                behaviorstTable.Controls.Add(btn, 1, l);
            }

            #endregion

            itemProperties.Add(Item.Variables.PositionX, positionX);
            itemProperties.Add(Item.Variables.PositionY, positionY);
            itemProperties.Add(Item.Variables.MyTrigger, tlpMyTrigger);
            itemProperties.Add(Item.Variables.StandAloneBehavior, behaviorstTable);
        }

        private void InitializeCTileComponents(CTile cTile)
        {
            cTileProperties = new Dictionary<CTile.Variables, Control>();

            TextBox positionX = new TextBox();
            positionX.Text = "";

            TextBox positionY = new TextBox();
            positionY.Text = "";

            List<Trigger> triggers = LevelEditorDAO.GetInstance().GetAllTriggers();
            #region My Trigger
            TableLayoutPanel tlpMyTrigger = new TableLayoutPanel();
            tlpMyTrigger.RowCount = cTile.MyTrigger.Count;
            tlpMyTrigger.ColumnCount = 1;
            tlpMyTrigger.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            tlpMyTrigger.AutoScroll = true;

            for (int line = 0; line < cTile.MyTrigger.Count; line++)
            {
                Label l = new Label();
                l.Text = cTile.MyTrigger[line].Name;
                tlpMyTrigger.Controls.Add(l, 0, line);
            }
            #endregion

            #region StandAloneBehavior

            TableLayoutPanel behaviorstTable = new TableLayoutPanel();
            behaviorstTable.RowCount = cTile.standAloneBehaviors.Count;
            behaviorstTable.ColumnCount = 2;
            behaviorstTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            behaviorstTable.AutoScroll = true;

            for (int l = 0; l < cTile.standAloneBehaviors.Count; l++)
            {
                Label label = new Label();
                label.Text = cTile.standAloneBehaviors[l].Name;

                Button btn = new Button();
                btn.Text = "D";
                btn.Width = 20;
                btn.Height = 20;

                Behavior b = cTile.standAloneBehaviors[l];
                btn.Click += new EventHandler((sender, e) => deleteStandAlnoBehavior(sender, e, b, cTile));
                label.DoubleClick += new EventHandler((sender, e) => alterLoneBehavior(sender, e, b));

                if (b.IsWalk)
                {
                    MenuItem mItem = new MenuItem("Show Waypoints");
                    mItem.Click += new EventHandler((sender, e) => mItem_Click(sender, e, b));
                    label.ContextMenu = new System.Windows.Forms.ContextMenu(new MenuItem[] { mItem });
                }

                behaviorstTable.Controls.Add(label, 0, l);
                behaviorstTable.Controls.Add(btn, 1, l);
            }

            #endregion

            cTileProperties.Add(CTile.Variables.PositionX, positionX);
            cTileProperties.Add(CTile.Variables.PositionY, positionY);
            cTileProperties.Add(CTile.Variables.MyTrigger, tlpMyTrigger);
            cTileProperties.Add(CTile.Variables.StandAloneBehavior, behaviorstTable);
        }


        #region Door
        private void InitializeDoorComponents(Door door)
        {
            doorProperties = new Dictionary<Door.Variables, Control>();

            TextBox positionX = new TextBox();
            positionX.Text = "";

            TextBox positionY = new TextBox();
            positionY.Text = "";

            TextBox nameRoom = new TextBox();
            nameRoom.Text = "";

            CheckBox isOpen = new CheckBox();
            isOpen.Checked = true;

            CheckBox isClosedForever = new CheckBox();
            isClosedForever.Checked = false;

            CheckBox isBlocked = new CheckBox();
            isBlocked.Checked = false;

            TextBox loadNextPartLevel = new TextBox();
            loadNextPartLevel.Text = "";

            #region StandAloneBehavior

            TableLayoutPanel behaviorstTable = new TableLayoutPanel();
            behaviorstTable.RowCount = door.standAloneBehaviors.Count;
            behaviorstTable.ColumnCount = 2;
            behaviorstTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            behaviorstTable.AutoScroll = true;

            for (int l = 0; l < door.standAloneBehaviors.Count; l++)
            {
                Label label = new Label();
                label.Text = door.standAloneBehaviors[l].Name;

                Button btn = new Button();
                btn.Text = "D";
                btn.Width = 20;
                btn.Height = 20;

                Behavior b = door.standAloneBehaviors[l];
                btn.Click += new EventHandler((sender, e) => deleteStandAlnoBehavior(sender, e, b, door));
                label.DoubleClick += new EventHandler((sender, e) => alterLoneBehavior(sender, e, b));

                if (b.IsWalk)
                {
                    MenuItem mItem = new MenuItem("Show Waypoints");
                    mItem.Click += new EventHandler((sender, e) => mItem_Click(sender, e, b));
                    label.ContextMenu = new System.Windows.Forms.ContextMenu(new MenuItem[] { mItem });
                }

                behaviorstTable.Controls.Add(label, 0, l);
                behaviorstTable.Controls.Add(btn, 1, l);
            }

            #endregion

            doorProperties.Add(Door.Variables.PositionX, positionX);
            doorProperties.Add(Door.Variables.PositionY, positionY);
            doorProperties.Add(Door.Variables.NameRoom, nameRoom);
            doorProperties.Add(Door.Variables.IsOpen, isOpen);
            doorProperties.Add(Door.Variables.IsClosedForever, isClosedForever);
            doorProperties.Add(Door.Variables.IsBlocked, isBlocked);
            doorProperties.Add(Door.Variables.LoadNextPartLevel, loadNextPartLevel);
            doorProperties.Add(Door.Variables.StandAlonebehavior, behaviorstTable);
        }

        private void ShowDoorProperties(Door tile)
        {
            Door.Variables[] variables = (Door.Variables[])Enum.GetValues(typeof(Door.Variables));
            for (int k = 0; k < variables.Length; k++)
            {
                Label label = new Label();
                label.Text = variables[k].ToString();
                if ((doorProperties[variables[k]] as TextBox) != null)
                {
                    TextBox txt = (TextBox)doorProperties[variables[k]];

                    if (label.Text == Door.Variables.LoadNextPartLevel.ToString())
                    {
                        EventInfo eventInfo = txt.GetType().GetEvent("Click");
                        Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Clicked", false);
                        eventInfo.RemoveEventHandler(txt, d);

                        txt.Text = typeof(Door).GetProperty(variables[k].ToString()).GetValue(tile).ToString();
                        txt.Click += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), tile, typeof(Door).GetMethod(variables[k] + "Clicked"));
                    }
                    else
                    {
                        EventInfo eventInfo = txt.GetType().GetEvent("TextChanged");
                        Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Changed", false);
                        eventInfo.RemoveEventHandler(txt, d);

                        txt.Text = typeof(Door).GetProperty(variables[k].ToString()).GetValue(tile).ToString();
                        txt.TextChanged += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), tile, typeof(Door).GetMethod(variables[k] + "Changed"));
                    }
                }
                else if ((doorProperties[variables[k]] as CheckBox) != null)
                {
                    CheckBox txt = (CheckBox)doorProperties[variables[k]];

                    EventInfo eventInfo = txt.GetType().GetEvent("CheckedChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[k] + "Clicked", false);
                    eventInfo.RemoveEventHandler(txt, d);

                    txt.Checked = (Boolean)(typeof(Door).GetProperty(variables[k].ToString()).GetValue(tile));
                    txt.CheckedChanged += (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), tile, typeof(Door).GetMethod(variables[k] + "Clicked"));
                }

                this.Controls.Add(label);
                this.Controls.Add(doorProperties[variables[k]]);
            }
        }

        /// <summary>
        /// Aqui pode dar erro
        /// </summary>
        /// <param name="tile"></param>
        private void HideDoorProperties(Door tile)
        {
            Door.Variables[] variables = (Door.Variables[])Enum.GetValues(typeof(Door.Variables));
            int i = 0;
            for (int k = 0; k < this.Controls.Count; k++)
            {
                if (typeof(Label) == this.Controls[k].GetType()) continue;

                if ((this.Controls[k] as TextBox) != null)
                {

                    if (variables[i].ToString() != Door.Variables.LoadNextPartLevel.ToString())
                    {
                        EventInfo eventInfo = ((TextBox)this.Controls[k]).GetType().GetEvent("TextChanged");
                        Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[i] + "Changed", false);
                        eventInfo.RemoveEventHandler(((TextBox)this.Controls[k]), d);
                        i++;
                    }
                    else
                    {
                        EventInfo eventInfo = ((TextBox)this.Controls[k]).GetType().GetEvent("Click");
                        Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[i] + "Clicked", false);
                        eventInfo.RemoveEventHandler(((TextBox)this.Controls[k]), d);
                        i++;
                    }
                }
                else if ((this.Controls[k] as CheckBox) != null)
                {
                    EventInfo eventInfo = ((CheckBox)this.Controls[k]).GetType().GetEvent("CheckedChanged");
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler), tile, variables[i] + "Clicked", false);
                    eventInfo.RemoveEventHandler(((CheckBox)this.Controls[k]), d);
                    i++;
                }
            }
            this.Controls.Clear();
        }

        #endregion

    }
}
